package com.ah.proj.bus.user.servlet;

import java.awt.image.BufferedImage;
import java.util.*;

import javax.imageio.ImageIO;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;

import com.ah.proj.bean.User;
import com.ah.proj.bus.user.service.UserService;
import com.ah.proj.util.*;
import com.fasterxml.jackson.databind.ObjectMapper;

@WebServlet("/UserServlet")
public class UserServlet extends BaseServlet {
	UserService service = new UserService();

	private void regCheck(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String username = request.getParameter("username");
		boolean exist = service.isUserExist(username);
		// 构造Map类型的对象，用户表示检测结果
		Map<String, Object> map = new HashMap<String, Object>();
		if (exist) {
			map.put("userExsit", true);
		} else {
			map.put("userExsit", false);
		}
		ObjectMapper mapper = new ObjectMapper();
		// 转为json字符串，发送给用户
		System.out.println(username + ":" + mapper.writeValueAsString(map));
		mapper.writeValue(response.getWriter(), map);
	}

	private void verificationCode(HttpServletRequest request, HttpServletResponse response) throws Exception {
		HttpSession session = request.getSession();
		BufferedImage vc = ImgUtils.createVCode(session);
		ImageIO.write(vc, "JPG", response.getOutputStream());
	}

	private void regist(HttpServletRequest request, HttpServletResponse response) throws Exception {
		// 获取Session
		HttpSession session = request.getSession();
		// 获取参数(用户输入的验证码)
		String vcode = request.getParameter("vcode");
		if (!vcode.equals(session.getAttribute("vcode"))) {
			request.setAttribute("msg", "验证码错误！");
			// 转发，让页面显示错误消息提示
			request.getRequestDispatcher(Const.PAGE_REGIST).forward(request, response);
		} else {
			// 获取用户输入的用户名和密码
			String username = request.getParameter("username");
			String pwd = request.getParameter("pwd");
			// 此处还要做一次用户名存在判断
			boolean exist = service.isUserExist(username);
			if (exist) {
				request.setAttribute("msg", "注册失败 ，该用户名已被抢注！");
				// 转发，让页面显示错误消息提示
				request.getRequestDispatcher(Const.PAGE_REGIST).forward(request, response);
				return;
			}
			// 构造User对象
			User u = new User();
			u.setUserName(username);
			u.setPwd(pwd);
			if (service.register(u) > 0) {
				response.sendRedirect(Const.PAGE_LOGIN);
			} else {
				request.setAttribute("msg", "注册失败 ，请稍后重试！");
				// 转发，让页面显示错误消息提示
				request.getRequestDispatcher(Const.PAGE_REGIST).forward(request, response);
			}
		}
	}

	private void login(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String username = request.getParameter("username");
		String pwd = request.getParameter("pwd");
		System.out.println("username:" + username + " username:" + pwd);
		User u = new User();
		u.setUserName(username);
		u.setPwd(pwd);
		User login = service.login(u);
		if (login != null) {
			response.sendRedirect(Const.ACTION_LIST);
		} else {
			request.setAttribute("msg", "登录失败！");
			request.getRequestDispatcher(Const.PAGE_LOGIN).forward(request, response);
		}
	}
}
